/**
 * Created by WangBin on 2015/9/12.
 */
myApp.config(function ($routeProvider) {
    $routeProvider.when('/role/view', {
        templateUrl: 'role/view',
        controller: 'roleListController'
    }).when('/role/addUI', {
        templateUrl: 'role/editUI',
        controller: 'roleEditController'
    }).when('/role/editUI/:id', {
        templateUrl: 'role/editUI',
        controller: 'roleEditController'
    });
});
myApp.service('roleListService', function ($http) {
    var doRequest = function () {
        return $http.get('role/list');
    }
    return {
        roleList: function () {
            return doRequest();
        }
    };
});
myApp.controller('roleListController', function ($scope, $location, $http,roleListService) {
    roleListService.roleList().success(function (response) {
        $scope.roles = response;
    });
    $scope.editUI = function (id) {
        $location.path('/role/editUI/' + id);
    };
    $scope.addUI = function () {
        $location.path('/role/addUI');
    };
    $scope.deleteRole = function (id, description) {
        if (confirm('确定要删除"' + description + '"吗?')) {
            $http.get('role/delete/' + id).success(function () {
                alert('删除成功');
                roleListService.roleList().success(function (response) {
                    $scope.roles = response;
                });
            }).error(function () {
                alert('删除失败');
            });
        }
    };
});
myApp.controller('roleEditController', function ($scope, $location, $routeParams, $http) {
    if ($routeParams.id != null) {
        $http.get("role/getRole/" + $routeParams.id).success(function (response) {
            $scope.role = response;
        });
    }

    $scope.saveRole = function () {
        console.log($scope.role);
        $http.post('role/edit', $scope.role).success(function () {
            $location.path('/role/view');
        }).error(function () {
            alert('失败');
        });
    };
});